<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 鬼国二少 <guiguoershao@163.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

/**
 * 前台公共库文件
 * 主要定义前台公共函数库
 */

/**
 * 检测验证码
 * @param  integer $id 验证码ID
 * @return boolean     检测结果
 * @author 鬼国二少 <guiguoershao@163.com>
 */
function check_verify($code, $id = 1){
	$verify = new \Think\Verify();
	return $verify->check($code, $id);
}

/**
 * 获取列表总行数
 * @param  string  $category 分类ID
 * @param  integer $status   数据状态
 * @author 鬼国二少 <guiguoershao@163.com>
 */
function get_list_count($category, $status = 1){
    static $count;
    if(!isset($count[$category])){
        $count[$category] = D('Document')->listCount($category, $status);
    }
    return $count[$category];
}

/**
 * 获取段落总数
 * @param  string $id 文档ID
 * @return integer    段落总数
 * @author 鬼国二少 <guiguoershao@163.com>
 */
function get_part_count($id){
    static $count;
    if(!isset($count[$id])){
        $count[$id] = D('Document')->partCount($id);
    }
    return $count[$id];
}

/**
 * 获取导航URL
 * @param  string $url 导航URL
 * @return string      解析或的url
 * @author 鬼国二少 <guiguoershao@163.com>
 */
function get_nav_url($url){
    switch ($url) {
        case 'http' === substr($url, 0, 4):
            break;

        case '#' === substr($url, 0, 1):
            break;

        case 'javascript' === substr($url, 0, 10):
            break;

        default:
            $url = U($url);
            break;
    }
    return $url;
}

/**
 * 获取图片路径
 * @param $cover
 * @param string $default
 * @return string|完整的数据
 */
function getCoverUrl($cover, $default='')
{
    if (empty($cover)) {
        return $default;
    }
    if (substr($cover, 0, 4) == 'http') {
        return $cover;
    }

    if (is_numeric($cover)) {
        return __ROOT__ . get_cover($cover, 'path');
    }
    return __ROOT__.$cover;
}

/**
 * 获取列表的url
 * @param $cateId
 * @param string $module
 * @return string
 */
function getListUrl($cateId, $module='')
{
    $url = '';

    $depr = C('URL_PATHINFO_DEPR');
    switch (strtolower($module)) {
        //  资讯
        case 'news':
            $url = C('URL_ROUTER_ON') ? 'news'.$depr.'lists' : 'Home'.$depr.'News'.$depr.'lists';
            break;

        //  课程
        case 'curricula':
            $url = C('URL_ROUTER_ON') ? 'curricula'.$depr.'lists' : 'Home'.$depr.'Curricula'.$depr.'lists';
            break;

        //  师资
        case 'teacher':
            $url = C('URL_ROUTER_ON') ? 'teacher'.$depr.'lists' : 'Home'.$depr.'Teacher'.$depr.'lists';
    }
    return C('URL_ROUTER_ON') ? U($url.$depr.$cateId) : U($url, ['cateid'=>$cateId]);
}

/**
 * 获取详情页面的url地址
 * @param $detailId
 * @param string $module
 * @return string
 */
function getDetailUrl($detailId, $module='')
{
    $url = '';

    $depr = C('URL_PATHINFO_DEPR');
    switch (strtolower($module)) {
        case 'news':
            $url = C('URL_ROUTER_ON') ? 'news'.$depr.'detail' : 'Home'.$depr.'News'.$depr.'detail';
            break;

        case 'curricula':
            $url = C('URL_ROUTER_ON') ? 'curricula'.$depr.'detail' : 'Home'.$depr.'Curricula'.$depr.'detail';
            break;

        case 'about':
            switch ($detailId) {
                case '12':
                    $url = 'about'.$depr.'profile';
                    break;

                case '13':
                    $url = 'about'.$depr.'culture';
                    break;

                case '14':
                    $url = 'about'.$depr.'achievement';
                    break;

                case '15':
                    $url = 'about'.$depr.'contact';
                    break;

                default:
                    $url = '';
                    break;
            }
            return U($url);

        case 'tixi':
            switch ($detailId) {
                case '21':
                    $url = 'tixi'.$depr.'xiaoxue';
                    break;

                case '22':
                    $url = 'tixi'.$depr.'chuzhong';
                    break;

                case '23':
                    $url = 'tixi'.$depr.'gaozhong';
                    break;

                default:
                    $url = '';
                    break;
            }
            return U($url);
        default:
            $url = C('URL_ROUTER_ON') ? 'article'.$depr.'detail' : 'Home'.$depr.'Article'.$depr.'show';
            break;
    }

    return C('URL_ROUTER_ON') ? U($url .$depr . $detailId) : U($url, ['id'=>$detailId]);
}